import QtQuick 1.0
import com.nokia.symbian 1.1

ApplicationWindow {
    id: theAPP

    Component.onCompleted:{
        pageStack.push(mainPage);
        //deferred initialization for performance reason
        catalogModel.readDRMCatalog();
    }

    Page{
        id: mainPage
        tools: ToolBarLayout {
            ToolButton {
                iconSource: "toolbar-back"
                onClicked: Qt.quit()
            }
            ToolButton {
                id: buttonRestore
                iconSource: "toolbar-refresh"
                enabled: !catalogModel.isApplicationBusy
                ToolTip {
                    id: toolTipRestore
                    text: "Restore purchase sessions"
                    target: buttonRestore
                    visible: false
                }
                onPlatformPressAndHold: toolTipRestore.visible = true
                onPlatformReleased: toolTipRestore.visible = false
            }
            ToolButton {
                id: buttonDelete
                iconSource: "toolbar-delete"
                enabled: !catalogModel.isApplicationBusy
                ToolTip {
                    id: toolTipDelete
                    text: "Reset purchase sessions"
                    target: buttonDelete
                    visible: false
                }
                onPlatformPressAndHold: toolTipDelete.visible = true
                onPlatformReleased: toolTipDelete.visible = false
                onClicked: {
                    catalogModel.resetPurchases();
                }
            }
            ToolButton {
                iconSource: "gfx/icon_info.png"
                onClicked: theAPP.pageStack.push(infoPage)
            }
        }

        Flickable {
            anchors.fill: parent
            contentWidth: parent.width
            contentHeight: content.height

            Column{
                id: content
                spacing: 10

                ListView {
                    interactive: false
                    model: itemsToBuyModel
                    delegate: listToBuyDelegate
                    header: ListHeading {
                        ListItemText {
                            anchors.centerIn: parent
                            role: "SelectionSubTitle"
                            text: "Items to buy"
                        }
                    }
                    width: mainPage.width
                    // NOTE: incrementing items count is for accounting header
                    height: (mainPage.tools.implicitHeight + spacing) * (count + 1)
                    z:count // to overlap "Items you have" header if the list is empty
                }

                ListView {
                    interactive: false
                    model: itemsBoughtModel
                    delegate: listYouHaveDelegate
                    header: ListHeading {
                        ListItemText {
                            anchors.centerIn: parent
                            role: "SelectionSubTitle"
                            text: "Items you have"
                        }
                    }
                    width: mainPage.width
                    height: (mainPage.tools.implicitHeight + spacing) * (count + 1)
                }
            }
        }

        BusyIndicator {
            anchors.centerIn: parent
            visible: catalogModel.isApplicationBusy
            running: true
            width: 60
            height: 60
        }

        Component {
            id: listToBuyDelegate

            ListItem {
                subItemIndicator: true
                Column {
                    anchors.fill: parent.paddingItem

                    ListItemText {
                        role: "Title"
                        text: iap_productid // Title text from model
                        width: parent.width
                        color: iap_isBusy ? "darkred" : "darkgreen"
                    }
                }
                onClicked:{
                    catalogModel.buy(index);
                }
            }
        }

        Component {
            id: listYouHaveDelegate

            ListItem {
                Column {
                    anchors.fill: parent.paddingItem
                    ListItemText {
                        role: "Title"
                        text: iap_productid // Title text from model
                        width: parent.width
                    }
                }
            }
        }

        Page{
            id: infoPage
            TextArea{
                id: info
                anchors.margins: 30
                anchors.fill: parent
                readOnly: true
                property string link: "http://projects.developer.nokia.com/iap_sample_drm_case"

                text: "This is a mock game application project that demonstrates use " +
                      "of In-application Purchase API with Nokia DRM protected resources. " +
                      "You can use that application as skeleton for your project.<br/><br/>" +
                      "For more details please see the application project page : " +
                      "<p style=\"color: darkblue\">TryAndBuyExample</p>"
            }

            MouseArea{
                anchors.fill: info
                onClicked: Qt.openUrlExternally(info.link)
            }

            tools: ToolBarLayout {
                ToolButton {
                    iconSource: "toolbar-back"
                    onClicked: theAPP.pageStack.pop()
                }
            }
        }
    }
}
